package com.ciko.code.logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ciko.code.logic.bean.AddressBookInfo;
import com.ciko.code.logic.db.ContactsDBHelper2;
import com.ciko.code.logic.table.AddressBookInfoTable;

import java.util.ArrayList;
import java.util.List;


/**
 * 联系人 数据操作类
 * 
 * @author 刘宾
 * @date 2015年12月15日 下午3:12:16
 */
public class AddressBookInfoDao
{
	private static AddressBookInfoDao dao;
	private ContactsDBHelper2 dbHelper;
	
	private AddressBookInfoDao(Context context) {
		dbHelper = ContactsDBHelper2.getInstance(context);
	}
	
	public static AddressBookInfoDao getInstance(Context context) {
		dao = new AddressBookInfoDao(context);
		return dao;
	}
	
	public boolean add(AddressBookInfo bean) {
		try
		{
			final ContentValues values = dbHelper.getContentValues(ContactsDBHelper2.TYPE_INSERT, bean);
			dbHelper.getSqliteDatabaseW().insert(AddressBookInfoTable.table, null, values);
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return false;
	}
	
	public boolean addList(List<AddressBookInfo> list) {
		SQLiteDatabase db = null;
		try
		{
			db = dbHelper.getSqliteDatabaseW();
			for (int i = 0; i < list.size(); i++)
			{
				final ContentValues values = dbHelper.getContentValues(ContactsDBHelper2.TYPE_INSERT, list.get(i));
				db.insert(AddressBookInfoTable.table, null, values);
			}
			db.setTransactionSuccessful();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			try
			{
				db.endTransaction();
				db.close();
				dbHelper.closeSqliteDatabaseW();
			}
			catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	public boolean update(AddressBookInfo bean) {
		final String selection = AddressBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + AddressBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
		        bean.getContactId(),
		        String.valueOf(bean.getGroupId())
		};
		int v = 0;
		try
		{
			ContentValues values = dbHelper.getContentValues(ContactsDBHelper2.TYPE_UPDATE, bean);
			v = dbHelper.getSqliteDatabaseW().update(AddressBookInfoTable.table, values, selection, args);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return v > 0 ? true : false;
	}
	
	public boolean delete(String contactId, int groupId) {
		final String selection = AddressBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + AddressBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
		        contactId,
		        String.valueOf(groupId)
		};
		int v = dbHelper.getSqliteDatabaseW().delete(AddressBookInfoTable.table, selection, args);
		dbHelper.closeSqliteDatabaseW();
		return v > 0 ? true : false;
	}
	
	public int deleteByGroupId(int groupId) {
		final String selection = AddressBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
			String.valueOf(groupId)
		};
		int count = dbHelper.getSqliteDatabaseW().delete(AddressBookInfoTable.table, selection, args);
		dbHelper.closeSqliteDatabaseW();
		return count;
	}
	
	public int deleteAll() {
		int count = dbHelper.getSqliteDatabaseW().delete(AddressBookInfoTable.table, null, null);
		dbHelper.closeSqliteDatabaseW();
		return count;
	}
	
	public AddressBookInfo getById(String contactId, int groupId) {
		AddressBookInfo bean = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + AddressBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
		        contactId,
		        String.valueOf(groupId)
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(AddressBookInfo.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		
		return bean;
	}

	public AddressBookInfo getById(String contactId ) {
		AddressBookInfo bean = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.ACC_ID + " = ? ";
		final String[] args = new String[] {
				contactId
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(AddressBookInfo.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}

		return bean;
	}

	public AddressBookInfo getByuserId(String uId) {
		AddressBookInfo bean = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.USER_ID + " = ? ";
		final String[] args = new String[] {
				uId
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(AddressBookInfo.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}

		return bean;
	}


	public List<AddressBookInfo> getListByGroupId(int groupId) {
		List<AddressBookInfo> list = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
			String.valueOf(groupId)
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<AddressBookInfo>();
				while (c.moveToNext())
				{
					list.add(dbHelper.getObject(AddressBookInfo.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		
		return list;
	}
	
	public List<AddressBookInfo> getAll() {
		List<AddressBookInfo> list = null;
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, null, null, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<AddressBookInfo>();
				while (c.moveToNext())
				{
					list.add(dbHelper.getObject(AddressBookInfo.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		return list;
	}
	
	public List<AddressBookInfo> search(String string) {
		List<AddressBookInfo> list = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.CONTACT_NAME + " like '%" + string + "%'";
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, null, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<AddressBookInfo>();
				while (c.moveToNext())
				{
					list.add(dbHelper.getObject(AddressBookInfo.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		
		return list;
	}

	public List<AddressBookInfo> searchByPhone(String string) {
		List<AddressBookInfo> list = null;
		final String selection = AddressBookInfoTable.AddressBookInfoColums.MOBILE_NUMBER + " like '%" + string + "%'";
		Cursor c = dbHelper.getSqliteDatabaseR().query(AddressBookInfoTable.table, null, selection, null, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<AddressBookInfo>();
				while (c.moveToNext())
				{
					list.add(dbHelper.getObject(AddressBookInfo.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}

		return list;
	}
}
